package com.tanhua.manage.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.manage.interceptor.AdminThreadLocal;
import com.tanhua.manage.service.AdminService;
import com.tanhua.manage.util.NoAuthorization;
import com.tanhua.manage.vo.AdminVo;
import com.tanhua.manage.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 功能描述: 生成验证码图片
     *
     * @param
     * @param
     * @Return: void
     * @throws:
     */
    @GetMapping("/verification")
    @NoAuthorization
    public void verification(@RequestParam(name = "uuid") String uuid, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store,no-cache,must-revalidate");
        response.addHeader("Cache-Control", "post-check=0,pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
        String captcha = lineCaptcha.getCode();
        adminService.saveVerification(uuid, captcha);
        //log.info("申请了一个验证码：" + uuid + "  " + captcha);
        lineCaptcha.write(response.getOutputStream());

    }


    /**
     * 登陆
     *
     * @param param
     * @return
     */
    @PostMapping("login")
    @NoAuthorization
    public ResponseEntity<String> login(@RequestBody Map<String, String> param) {
        try {
            // 获取参数
            String username = param.get("username");
            String password = param.get("password");
            String code = param.get("verificationCode");
            String uuid = param.get("uuid");
            //返回token
            String token = this.adminService.login(username, password, code, uuid);
            // 不为空
            //System.out.println(token);
            return ResponseEntity.ok("{\"token\":\"" + token + "\"}");

        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult.ErrorResultBuilder builder = ErrorResult.builder().errCode("000000").errMessage("登录失败");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户基本信息
     *
     * @return
     */
    @PostMapping("profile")
    public ResponseEntity<AdminVo> profile() {
        try {

            AdminVo adminVo = adminService.queryAdminByToken();
            if (null != adminVo) {
                return ResponseEntity.ok(adminVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 退出
     *
     * @param token
     */
    @PostMapping("logout")
    private ResponseEntity<Void> logout(@RequestHeader("Authorization") String token) {
        //删除token
        this.adminService.removeToken(token);
        AdminThreadLocal.remove();
        return ResponseEntity.ok(null);


    }
}